//强制保留2位小数，如：2，会在2后面补上00.即2.00
function toDecimal2(x) {
  var f = parseFloat(x)
  if (isNaN(f)) {
    return false
  }
  var f = Math.round(x * 100) / 100
  var s = f.toString()
  var rs = s.indexOf('.')
  if (rs < 0) {
    rs = s.length
    s += '.'
  }
  while (s.length <= rs + 2) {
    s += '0'
  }
  return s
}
function routerLinkTo(link, num) {
  if (link && num == 1) {
    link = link.toString()
    this.$router.resolve(link)
  } else {
    this.$router.push(link)
  }
}

function checkEmail(str) {
  var re = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/
  if (re.test(str)) {
    return true
  } else {
    return false
  }
}
export function debounce(func, delay) {
  let timer
  return function(...args) {
    if (timer) {
      clearTimeout(timer)
    }
    timer = setTimeout(() => {
      func.apply(this, args)
    }, delay)
  }
}

function errorImg(event) {
  // console.log('event :>> ', event)
  let img = event.target
  // img.src = require('@/assets/img/noImg.png')
  img.src =
    ' '
  // console.log('img.src :>> ', img.src)
  // img.onerror = null //防止一直跳动
}

function downLoadFile(url) {
  //1.先创建一个a标签
  let a = document.createElement('a')
  //2.给a标签的href赋上下载地址
  a.href = url
  a.target = 'blank'
  //3.让a标签不显示在视图中
  a.style.display = 'none'
  //4.将a标签append到文档中
  document.body.appendChild(a)
  //5.a标签自点击
  a.click()
  //6.点击下载后，销毁这个节点
  document.body.removeChild(a)
}
export default {
  toDecimal2,
  routerLinkTo,
  checkEmail,
  debounce,
  errorImg,
  downLoadFile,
}
